package com.small.admin.controller;

import com.small.admin.dto.*;
import com.small.common.base.ConstantPattern;
import com.small.admin.config.constant.MsgAdmin;
import com.small.admin.service.RoleAdminService;
import com.small.admin.service.RoleService;
import com.small.common.config.admin.IAdminInfo;
import com.small.common.msg.Msg;
import com.small.common.msg.MsgData;
import com.small.common.msg.MsgPage;
import com.small.common.util.encrypt.MD5;
import com.small.common.util.val.group.ValAdd;
import com.small.common.util.val.group.ValUpdate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.small.admin.service.AdminService;
import com.small.admin.entity.Admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;

/**
 * 作者: fenny
 * 邮箱: 893146555@qq.com
 * 时间: 2017-10-17 15:28:29
 * 描述: 管理员信息
 */
@Validated
@RestController
@RequestMapping("admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private RoleService roleService;

    @Resource
    private RoleAdminService roleAdminService;

    @Resource
    private IAdminInfo iAdminInfo;

    @PostMapping
    public Msg add(@Validated(ValAdd.class) @RequestBody AdminStore admin) {
        if (accountExists(admin.getAccount())) {
            return MsgAdmin.TIP_ADMIN_EXISTS;
        }
        admin.setPassword(MD5.encrypt32(admin.getPassword()));
        admin.setIsSystem(0);
        adminService.add(admin);
        return MsgAdmin.SUCCESS;
    }

    @PostMapping("deletes")
    public Msg deletes(@Valid @RequestBody AdminDelete adminDelete) {
        adminService.deletes(adminDelete);
        return MsgAdmin.SUCCESS;
    }

    @PutMapping
    public Msg update(@Validated(ValUpdate.class) @RequestBody AdminStore admin) {
        if (StringUtils.isNotEmpty(admin.getPassword())) {
            admin.setPassword(MD5.encrypt32(admin.getPassword()));
        } else {
            //不更新密码
            admin.setPassword(null);
        }
        adminService.update(admin);
        return MsgAdmin.SUCCESS;
    }

    @GetMapping("getName/{id}")
    public Msg getName(@PathVariable("id") long id) {
        //获取指定用户的名称
        Msg msg = new Msg();
        msg.setCode(1);
        Admin adminW = new Admin();
        adminW.setId(id);
        adminW = adminService.selectOne(adminW);
        msg.setMsg(adminW.getName());
        return msg;
    }

    @GetMapping("page")
    public MsgPage<Admin> page(AdminPage adminPage) {
        return adminService.selectByPage(adminPage);
    }

    @GetMapping("accountExists")
    public boolean accountExists(@Pattern(regexp = ConstantPattern.VAL_ACCOUNT) @RequestParam String account) {
        Admin adminWhere = new Admin();
        adminWhere.setAccount(account);
        return adminService.selectCount(adminWhere) > 0;
    }

    @PostMapping("modifyPwd")
    public Msg modifyPwd(HttpServletRequest request, @Valid @RequestBody AdminModifyPwd adminModifyPwd) {
        return adminService.modifyPwd(adminModifyPwd, iAdminInfo.getId(request));
    }

    @GetMapping("loadRole")
    public MsgData<LoadRole> loadRole(@RequestParam(defaultValue = "0") long adminId) {
        LoadRole loadRole = new LoadRole();
        //获得所有角色
        loadRole.setAllRole(roleService.selectListAll());
        //获得指定用户已有的角色
        loadRole.setHaveRole(roleAdminService.selectByAdminId(adminId));
        return new MsgData<>(loadRole);
    }
}